package com.carp.springframework.core.io;

import cn.hutool.core.lang.Assert;
import com.carp.springframework.util.ClassUtils;

import java.io.InputStream;
import java.util.Optional;

/**
 * @author lile
 * @date 2023/2/13
 */
public class ClassPathResource implements Resource {

    private final String path;

    private final ClassLoader classLoader;

    public ClassPathResource(String path) {
        this(path, null);
    }

    public ClassPathResource(String path, ClassLoader classLoader) {
        Assert.notNull(path, "Path must not be null");
        this.path = path;
        this.classLoader = Optional.ofNullable(classLoader).orElse(ClassUtils.getDefaultClassLoader());
    }

    @Override
    public InputStream getInputStream() {
        return classLoader.getResourceAsStream(path);
    }

}
